package com.luhuiguo.cms.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.luhuiguo.cms.client.AppConstants;
import com.luhuiguo.cms.client.AppMessages;
import com.luhuiguo.cms.client.AppValidationMessages;
import com.luhuiguo.cms.client.presenter.EditArticlePresenter;
import com.luhuiguo.gwt.ckeditor.client.CKConfig;
import com.luhuiguo.gwt.ckeditor.client.CKEditor;
import com.luhuiguo.gwt.ckeditor.client.Toolbar;
import com.luhuiguo.gwt.ckeditor.client.ToolbarLine;
import com.luhuiguo.gwt.ckeditor.client.CKConfig.TOOLBAR_OPTIONS;
import com.luhuiguo.gwt.validation.client.DefaultValidationProcessor;
import com.luhuiguo.gwt.validation.client.ValidationProcessor;
import com.luhuiguo.gwt.validation.client.actions.FocusAction;
import com.luhuiguo.gwt.validation.client.actions.StyleAction;
import com.luhuiguo.gwt.validation.client.description.PopupDescription;
import com.luhuiguo.gwt.validation.client.description.PopupDescription.Location;
import com.luhuiguo.gwt.validation.client.validators.standard.RegularExpressionValidator;
import com.luhuiguo.gwt.validation.client.validators.strings.StringLengthValidator;

public class EditArticleView extends Composite implements EditArticlePresenter.Display {

    private static EditArticleViewUiBinder uiBinder = GWT.create(EditArticleViewUiBinder.class);

    interface EditArticleViewUiBinder extends UiBinder<Widget, EditArticleView> {
    }

    @UiField
    TextBox titleField;

    @UiField
    TextBox subjectField;

    @UiField
    TextBox authorField;

    @UiField
    TextBox sourceField;

    @UiField
    TextBox keywordsField;

    @UiField
    TextBox descriptionField;

    @UiField(provided = true)
    CKEditor contentField;

    // private ListBox statusField;
    // private DateBox publishDateField;

    @UiField
    Button submitButton;
    @UiField
    Button cancelButton;

    private AppConstants constants;

    private AppMessages messages;

    private ValidationProcessor validator;

    private PopupDescription description;

    @Inject
    public EditArticleView(AppConstants constants, AppMessages messages, AppValidationMessages validationMessages) {
        this.constants = constants;
        this.messages = messages;

        CKConfig cfg = new CKConfig();

        ToolbarLine line = new ToolbarLine();

        TOOLBAR_OPTIONS[] t1 = { TOOLBAR_OPTIONS.Source, TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Cut, TOOLBAR_OPTIONS.Copy,
                TOOLBAR_OPTIONS.Paste, TOOLBAR_OPTIONS.PasteText, TOOLBAR_OPTIONS.PasteFromWord, TOOLBAR_OPTIONS._,
                TOOLBAR_OPTIONS.Undo, TOOLBAR_OPTIONS.Redo, TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Find,
                TOOLBAR_OPTIONS.Replace, TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.SelectAll, TOOLBAR_OPTIONS.RemoveFormat,
                TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Bold, TOOLBAR_OPTIONS.Italic, TOOLBAR_OPTIONS.Underline,
                TOOLBAR_OPTIONS.Strike, TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Subscript, TOOLBAR_OPTIONS.Superscript,
                TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.NumberedList, TOOLBAR_OPTIONS.BulletedList, TOOLBAR_OPTIONS._,
                TOOLBAR_OPTIONS.Outdent, TOOLBAR_OPTIONS.Indent, TOOLBAR_OPTIONS.Blockquote, TOOLBAR_OPTIONS._,
                TOOLBAR_OPTIONS.JustifyLeft, TOOLBAR_OPTIONS.JustifyCenter, TOOLBAR_OPTIONS.JustifyRight,
                TOOLBAR_OPTIONS.JustifyBlock };
        line.addAll(t1);

        ToolbarLine line2 = new ToolbarLine();

        TOOLBAR_OPTIONS[] t2 = { TOOLBAR_OPTIONS.Styles, TOOLBAR_OPTIONS.Format, TOOLBAR_OPTIONS.Font,
                TOOLBAR_OPTIONS.FontSize, TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.TextColor, TOOLBAR_OPTIONS.BGColor,
                TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Link, TOOLBAR_OPTIONS.Unlink, TOOLBAR_OPTIONS.Anchor,
                TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Image, TOOLBAR_OPTIONS.Flash, TOOLBAR_OPTIONS.Table,
                TOOLBAR_OPTIONS.HorizontalRule, TOOLBAR_OPTIONS.Smiley, TOOLBAR_OPTIONS.SpecialChar,
                TOOLBAR_OPTIONS.PageBreak, TOOLBAR_OPTIONS._, TOOLBAR_OPTIONS.Maximize, TOOLBAR_OPTIONS.ShowBlocks,
                TOOLBAR_OPTIONS.Preview };
        line2.addAll(t2);

        Toolbar t = new Toolbar();
        t.add(line);
        t.addSeparator();
        t.add(line2);

        cfg.setToolbar(t);
        cfg.setWidth("800px");
        cfg.setHeight("300px");
        cfg.setResizeEnabled(true);

        contentField = new CKEditor(cfg);

        initWidget(uiBinder.createAndBindUi(this));

        validator = new DefaultValidationProcessor(validationMessages);
        description = new PopupDescription(validationMessages, Location.RIGHT);

        description.addDescription("titleHint", titleField);

        FocusAction fAction = new FocusAction();
        StyleAction sAction = new StyleAction(constants.validationFailedBorder());
        validator.addValidators("title", new StringLengthValidator(titleField, 1, 100).addActionForFailure(fAction)
                .addActionForFailure(sAction));

        GWT.log("EditArticleView created");
    }

    @Override
    public HasValue<String> getTitleField() {
        return titleField;
    }

    @Override
    public HasValue<String> getSubjectField() {
        return subjectField;
    }

    @Override
    public HasValue<String> getAuthorField() {
        return authorField;
    }

    @Override
    public HasValue<String> getSourceField() {
        return sourceField;
    }

    @Override
    public HasValue<String> getKeywordsField() {
        return keywordsField;
    }

    @Override
    public HasValue<String> getDescriptionField() {
        return descriptionField;
    }

    @Override
    public HasHTML getContentField() {
        return contentField;
    }

    // @Override
    // public ListBox getStatusField() {
    // return statusField;
    // }

    // @Override
    // public HasValue<Date> getPublishDateField() {
    // return publishDateField;
    // }

    @Override
    public HasClickHandlers getSubmitButton() {
        return submitButton;
    }

    @Override
    public HasClickHandlers getCancelButton() {
        return cancelButton;
    }

    @Override
    public ValidationProcessor getValidator() {
        return validator;
    }

    @Override
    public void setLoading(boolean loading) {
        if (loading) {
            // loading.show();
            submitButton.setEnabled(false);
            cancelButton.setEnabled(false);
        } else {
            // loading.hide();
            submitButton.setEnabled(true);
            cancelButton.setEnabled(true);
        }
    }

    @Override
    public boolean validate() {
        return validator.validate();
    }

    @Override
    public Widget asWidget() {
        return this;
    }

}
